package com.cn.transfer.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvSaSaleDelivery;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.PurchaseOrderDetail;
import com.cn.transfer.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.http.Header;

import java.util.*;

@RestController
@Slf4j
public class TestController {
    @Autowired
    private AaPersonMapper aaPersonMapper;
    @Autowired
    private PuPurchaseOrderBMapper orderbMapper;
    @Autowired
    private VvSaSaleOrderMapper vvSaSaleOrderMapper;
    @Autowired
    private VvPuPurchaseRequisitionMapper vvPuPurchaseRequisitionMapper;
    @Autowired
    private VvPuPurchaseRequisitionBMapper vvPuPurchaseRequisitionBMapper;
    @Autowired
    private VvSaSaleDeliveryMapper vvSaSaleDeliveryMapper;
    @RequestMapping("/test")
    public String test(){


//        headerList.add(new BasicHeader("Authorization", "Bearer " + apiKey));
//        headerList.add(new BasicHeader("Content-Type", "application/json;charset=utf-8"));

//        Header[] originalHeaders = new Header[2];
//        originalHeaders[0] = new BasicHeader("Authorization", "Bearer " + KeyConstant.JDY_KEY);
//        originalHeaders[1] = new BasicHeader("Content-Type", "application/json;charset=utf-8");
//        Map<String,Object> parameter=new HashMap<>();
//        parameter.put("app_id","63a001b443d27b000a61b0c0");
//        parameter.put("entry_id","63a001b7375bcb0008b457d0");
//        parameter.put("limit",20);
//        String jsonStr = JSONUtil.toJsonStr(parameter);
//        JSONObject jsonStr1 = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", originalHeaders, jsonStr);
//        Header[] headers = ConnmeUtils.createHeaders();
//        Map<String, Object> parameter = new HashMap<>();
//        parameter.put("app_id", "64f00ef131adb70007ad6e6d");
//        parameter.put("entry_id", "64f00f0e68761f0007719cf9");
//
//        Map<String ,Object> filter=new HashMap<>();
//        filter.put("rel","and");
//        Map<String ,Object> cond =new HashMap<>();
//        cond.put("field","djbh");
//        cond.put("type","string");
//        cond.put("method","eq");
//        List<String> values =new ArrayList<>();
//        values.add("C02-2023-09-0005");
//        cond.put("value",values);
//        List<Map<String,Object>> c=new ArrayList<>();
//        c.add(cond);
//        filter.put("cond",c);
//        parameter.put("filter",filter);
//        String jsonSale = JSONUtil.toJsonStr(parameter);
//        log.info("采购订单弃审时：==== "+jsonSale);
//        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
//        String data = jdySale.getStr("data");
//        JSONArray objects = JSONUtil.parseArray(data);
//        List<JdyPurchaseOrder> jdyPurchaseOrders = JSONUtil.toList(objects, JdyPurchaseOrder.class);
//        log.info(jdyPurchaseOrders.toString());
//        log.info(jdyPurchaseOrders.get(0).get_id());
//        log.info("采购订单弃审时简道云返回的数据：====" + jdySale);

     //   String jsonSale = JSONUtil.toJsonStr(stringObjectMap);
//        VvSaSaleOrder saleOrder = new VvSaSaleOrder(
//                "34401",
//                "AB008-2023-09-292",
//                new Date(), // 你需要提供合适的 Date 对象
//                "普通销售",
//                "河南-鑫派五金",
//                "国内展-CIFF",
//                "Delivery Method",
//                "河南-鑫派五金",
//                "PRO国内销售部",
//                "Sales Person",
//                "Warehouse",
//                "人民币",
//                "1.00000000000000",
//                new Date(), // 你需要提供合适的 Date 对象
//                "0.0000",
//                "Ordering Shop",
//                "Shipping Location",
//                "Order Type",
//                "Customer Region",
//                "Customer Category",
//                "Self Operated Contract"
//        );
//
//        vvSaSaleOrderMapper.insertVVSaleOrder(saleOrder);
//        Map<String, Object> parameters = new HashMap<>();
//        parameters.put("app_id", "6500165a60a971000ab0ad28");
//        parameters.put("entry_id", "65015e41ddd8340007a1698d");
//        Header[] originalHeaders = new Header[2];
//        originalHeaders[0] = new BasicHeader("Authorization", "Bearer " + KeyConstant.JDY_KEY);
//        originalHeaders[1] = new BasicHeader("Content-Type", "application/json;charset=utf-8");
//        List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameters, originalHeaders, "AB008-2022-06-050","documentnumber");
        List<VvSaSaleDelivery> notNullList=vvSaSaleDeliveryMapper.byLists();


        return JSONUtil.toJsonStr(notNullList);
    }


    // 模拟获取订单详情的方法，实际中需替换为实际数据源的获取
    public static List<PurchaseOrderDetail> getOrderDetails() {
        List<PurchaseOrderDetail> orderDetails = new ArrayList<>();

        // 添加示例订单详情对象到列表
        orderDetails.add(new PurchaseOrderDetail("AD.0303 打包带", "33333A2", "AD.0303 打包带", "透明，适用于仓库打包产品，拓臣仓库", "透明，适用于仓库打包产品，拓臣仓库", "..", "物料", "物料", "0", 10, "卷", 0, 0, 0.13, 0, 0, "未维护"));
        orderDetails.add(new PurchaseOrderDetail("AP.0101 下轨免拉手", "AP.0101.A0011", "AP.0101 下轨免拉手", "L3m，铝合金，古铜拉丝", "L3m，铝合金，古铜拉丝", "1支/袋，20支/箱", "BSN款", "BSN款", "赖若升SAM", 10, "支", 8.8496, 10, 0.13, 10, 100, null));

        return orderDetails;
    }








    public List<JdyPurchaseOrder> saveOrder(String voucherCode){
        Header[] headers = ConnmeUtils.createHeaders();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", "64f00ef131adb70007ad6e6d");
        parameter.put("entry_id", "64f00f0e68761f0007719cf9");
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        Map<String ,Object> cond =new HashMap<>();
        cond.put("field","djbh");
        cond.put("type","string");
        cond.put("method","eq");
        List<String> values =new ArrayList<>();
        values.add(voucherCode);
        cond.put("value",values);
        List<Map<String,Object>> conds=new ArrayList<>();
        conds.add(cond);
        filter.put("cond",conds);
        parameter.put("filter",filter);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        log.info("采购订单弃审时：==== "+jsonSale);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        log.info("采购订单查询结果"+data);
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, JdyPurchaseOrder.class);
    }

}
